<template>
  <main-page>
    <div slot="body">
      <el-form :inline="true" :model="search" size="mini">
        <el-form-item>
          <el-input v-model="search.nickName" placeholder="昵称" autocomplete="off" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-select v-model="search.sex" style="width:100%" placeholder="性别" clearable>
            <el-option key="男" label="男生" value="男"></el-option>
            <el-option key="女" label="女生" value="女"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="danger" @click="getUsersList">搜索</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="tableData" style="width: 100%" tooltip-effect="dark">
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="VIP到期">
                <span> <el-tag type="info">{{ props.row.vipEndTime | datetime }}</el-tag> </span>
              </el-form-item>
              <el-form-item label="金币数">
                <span>
                  <el-tag type="info">{{ props.row.gold }} 个</el-tag>
                </span>
              </el-form-item>
              <el-form-item label="QQ">
                <span>{{ props.row.qq }}</span>
              </el-form-item>
              <el-form-item label="微博">
                <span>{{ props.row.weibo }}</span>
              </el-form-item>
              <el-form-item label="推送Key">
                <span>{{ props.row.pushKey }}</span>
              </el-form-item>
              <el-form-item label="imei">
                <span>{{ props.row.imei }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column prop="id" label="编号" width="80"></el-table-column>
        <el-table-column prop="nickName" label="昵称"></el-table-column>
        <el-table-column prop="sex" label="性别"></el-table-column>
        <el-table-column label="头像">
          <template slot-scope="scope">
            <el-image style="width: 60px;" :src="scope.row.avator" fit="contain"></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="decodeTel" label="联系方式"></el-table-column>
        <el-table-column prop="email" label="邮箱"></el-table-column>
        <el-table-column label="上次登录时间">
          <template slot-scope="scope">{{scope.row.lastLoginTime | datetime}}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="danger"
              icon="el-icon-place"
              circle
              title="调整"
              size="small"
              @click="handleClick2Edit(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination :listNum="listNum" @currentChange="handleCurrentChange"></pagination>

      <el-dialog title="会员充值管理" :visible.sync="dialogFormVisible" width="40%">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px" class="demo-ruleForm">
          <el-form-item label="充值类型" prop="isVip">
            <el-radio-group v-model="form.isVip">
              <el-radio :label="1">金币充值</el-radio>
              <el-radio :label="2">VIP充值</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="充值价值" prop="obtain">
            <el-input-number v-model="form.obtain" :min="0"></el-input-number>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="onFormSubmit">提交</el-button>
        </div>
      </el-dialog>
    </div>
  </main-page>
</template>

<script>
import { getUsersList, updateObtainById } from "api/UsersApi";

import { config } from "assets/js/Data";

export default {
  name: "UsersList",
  data() {
    return {
      search: {
        currentIndex: 1,
        nickName: "",
        sex: ""
      },
      tableData: [],
      listNum: 0,
      config: config,
      dialogFormVisible: false,
      form: {
        id: "",
        isVip : 1,
        obtain: 0
      },
      rules: {
        isVip: [{required: true, message: "请选择充值类型"}],
        obtain: [{required: true, message: "请输入充值价值"}]
      }
    };
  },
  methods: {
    getUsersList() {
      getUsersList(this.search).then(res => {
        if (res) {
          this.tableData = res.datas;
          this.listNum = res.pagination.totalCount;
        }
      });
    },
    handleCurrentChange (page) {
      this.search.currentIndex = page
      this.getUsersList()
    },
    handleClick2Edit (row) {
      this.dialogFormVisible = true
      this.form.id = row.id
    },
    onFormSubmit () {
      this.$refs.form.validate(valid => {
        if (valid) {
          updateObtainById (this.form)
            .then(res => {
              this.$base.showOk();
              this.dialogFormVisible = false;
              this.getUsersList();
            })
        }
      });
    }
  },
  created() {
    this.getUsersList();
  }
};
</script>
<style lang="stylus" scoped>
.demo-table-expand {
  font-size: 0;
}

.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}

.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>